﻿using System;
using Kaiser.Models;
using Kaiser.Modules.Hiner.Models.Order.SearchModels;
using Kaiser.Modules.Hiner.Repositories;
using Kaiser.Modules.Hiner.Services;
using System.Collections.Generic;
using System.Data;
using Kaiser.Extensions;
namespace Kaiser.Modules.Hiner.Models.Order
{
    public class Order : IOrderService
    {
        private readonly IOrderRepositorie _orderRepositorie;
        public Order(IOrderRepositorie orderRepositorie)
        {
            _orderRepositorie = orderRepositorie;
        }

        public DataGrid<OrderBase> SearchOrder(OrderSearch search, PagingInfo pageinfo)
        {
            try
            {
                return _orderRepositorie.SearchOrder(search, pageinfo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool OrderDelivery(string orderId)
        {
            try
            {

               return  _orderRepositorie.OrderDelivery(orderId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Insert(Models.Order.OrderBase model)
        {
            try
            {
                _orderRepositorie.Insert(model);
                for (int i = 0; i < model.OrderDetailList.Count; i++)
                {
                    _orderRepositorie.DeleteOrderDetail(model.OrderId);
                    model.OrderDetailList[i].OrderDetailCreateDateTime = DateTime.Now;
                    model.OrderDetailList[i].OrderDetailId = Guid.NewGuid().GenerateGuid().ToString();
                    model.OrderDetailList[i].OrderId = model.OrderId;
                    _orderRepositorie.InsertOrderDetail(model.OrderDetailList[i]);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(Models.Order.OrderBase model)
        {
            try
            {
                _orderRepositorie.DeleteOrderDetail(model.OrderId);
                for (int i = 0; i < model.OrderDetailList.Count; i++)
                {
                    model.OrderDetailList[i].OrderDetailCreateDateTime = DateTime.Now;
                    model.OrderDetailList[i].OrderDetailId = Guid.NewGuid().GenerateGuid().ToString();
                    model.OrderDetailList[i].OrderId = model.OrderId;
                    _orderRepositorie.InsertOrderDetail(model.OrderDetailList[i]);
                }
                _orderRepositorie.Update(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Delete(object primaryId)
        {
            try
            {
                if (_orderRepositorie.GetOrderDetailList(primaryId.ToString()).Count > 0)
                {
                    throw new Exception("该表有关联数据，无法删除");
                }
                else
                {
                  return  _orderRepositorie.Delete(primaryId);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Models.Order.OrderBase Get(object primaryId)
        {
            try
            {
                OrderBase orderBase = _orderRepositorie.Get(primaryId);
                orderBase.OrderDetailList = _orderRepositorie.GetOrderDetailList(primaryId.ToString() );
                return orderBase;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public void DeleteOrderDetail(string orderID)
        {
            try
            {
                _orderRepositorie.DeleteOrderDetail(orderID);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public void InsertOrderDetail(OrderDetail model)
        {
            try
            {
                _orderRepositorie.InsertOrderDetail(model);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public IList<OrderDetail> GetOrderDetailList(string orderID)
        {
            try
            {
                return _orderRepositorie.GetOrderDetailList(orderID);
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        public bool OrderConfirm(string orderId)
        {
            try
            {
                return _orderRepositorie.OrderConfirm(orderId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public int DeleteList(IList<string> ids)
        {
            throw new NotImplementedException();
        }
    }
}
